﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Resources;
using LockCrypt.Core;
using LockCrypt.DAL;

namespace LockCrypt.Mobile {
    /// <summary>
    /// A form for managing account types.
    /// </summary>
    public partial class FrmManageTypes : Form {
        private List<ITemplate> Templates { get; set; }
        private bool cancelDialog = false;
        private BindingSource bsTypes = new BindingSource();

        public FrmManageTypes() {
            InitializeComponent();
            Templates = DalConfigurationManager.Provider.GetTemplates();
            //this.types.Sort();
            bsTypes.DataSource = this.Templates;
            lstTypes.DataSource = bsTypes;
            lstTypes.DisplayMember = "Name";

            itmSave.Click += (s, e) => {
                this.DialogResult = DialogResult.OK;
            };
            itmCancel.Click += (s, e) => {
                this.cancelDialog = true;
                this.DialogResult = DialogResult.Cancel;
            };
            this.Closing += delegate(object sender, CancelEventArgs e) {
                if(!cancelDialog)
                    this.DialogResult = DialogResult.OK;
            };
            mnuCtxTypeList.Popup += (s, e) => {
                if(lstTypes.SelectedItem == null) {
                    itmCtxEditType.Enabled = false;
                    itmCtxDeleteType.Enabled = false;
                } else {
                    itmCtxEditType.Enabled = true;
                    itmCtxDeleteType.Enabled = true;
                }
            };
            itmNewType.Click += new EventHandler(itmNewType_Click);
            itmEditType.Click += new EventHandler(itmEditType_Click);
            itmDeleteType.Click += new EventHandler(itmDeleteType_Click);
            itmCtxNewType.Click += new EventHandler(itmNewType_Click);
            itmCtxEditType.Click += new EventHandler(itmEditType_Click);
            itmCtxDeleteType.Click += new EventHandler(itmDeleteType_Click);
            UpdateEnabledState();
        }

        /// <summary>
        /// Adds a new type to the list.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void itmNewType_Click(object sender, EventArgs e) {
            using(FrmEditType frmEditType = new FrmEditType(Templates)) {
                if(frmEditType.ShowDialog() == DialogResult.OK) {
                    try {
                        DalConfigurationManager.Provider.AddTemplates(new[] { frmEditType.SelectedTemplate }, null);
                        this.Templates.Add(frmEditType.SelectedTemplate);
                        bsTypes.ResetBindings(false);
                        UpdateEnabledState();
                    } catch(Exception ex) {
                        MessageBox.Show(ex.Message, I18nUtils.GetString("Errors", "Error"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    }
                }
            }
        }

        /// <summary>
        /// Shows a form to edit the currently selected type.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void itmEditType_Click(object sender, EventArgs e) {
            if(bsTypes.Count > 0) {
                ITemplate templateToEdit = lstTypes.SelectedValue as ITemplate;
                if(!templateToEdit.CanUserEdit) {
                    MessageBox.Show(I18nUtils.GetString("Errors", "TemplateProtected"), I18nUtils.GetString("Errors", "Error"), MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
                } else {
                    using(FrmEditType frmEditType = new FrmEditType(Templates, templateToEdit)) {
                        if(frmEditType.ShowDialog() == DialogResult.OK) {
                            try {
                                DalConfigurationManager.Provider.EditTemplate(frmEditType.SelectedTemplate);
                                bsTypes.Remove(lstTypes.SelectedValue);
                                this.Templates.Add(frmEditType.SelectedTemplate);
                                bsTypes.ResetBindings(false);
                                UpdateEnabledState();
                            } catch(Exception ex) {
                                MessageBox.Show(ex.Message, I18nUtils.GetString("Errors", "ErrorSaving"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Deletes the currently selected item.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void itmDeleteType_Click(object sender, EventArgs e) {
            if(bsTypes.Count > 0) {
                ITemplate templateToEdit = lstTypes.SelectedValue as ITemplate;
                if(!templateToEdit.CanUserEdit) {
                    MessageBox.Show(I18nUtils.GetString("Errors", "TemplateProtected"), I18nUtils.GetString("Errors", "Error"), MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
                } else if(MessageBox.Show(I18nUtils.GetString("Strings", "DeleteTemplateDesc"), I18nUtils.GetString("Strings", "ConfirmDeletion"), MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes) {
                    try {
                        DalConfigurationManager.Provider.DeleteTemplate(templateToEdit.ID);
                        bsTypes.Remove(lstTypes.SelectedValue);
                        bsTypes.ResetBindings(false);
                        UpdateEnabledState();
                    } catch(Exception ex) {
                        MessageBox.Show(ex.Message, I18nUtils.GetString("Errors", "Error"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    }
                }
            }
        }

        /// <summary>
        /// Enables the 'Edit' and 'Delete' menu items if there are items in the list.
        /// </summary>
        private void UpdateEnabledState() {
            if(bsTypes.Count > 0) {
                itmEditType.Enabled = true;
                itmDeleteType.Enabled = true;
            } else {
                itmEditType.Enabled = false;
                itmDeleteType.Enabled = false;
            }
        }
    }
}